<?php

namespace App\Models;

use App\Models\CommonModel as Model;

/**
 * Class UserTicketModel
 * @package App\Models
 * @version May 9, 2018, 1:33 pm UTC
 *
 * @property int user_id
 * @property int ticket_id
 * @property int city_id
 */
class UserTicketModel extends Model
{
    public $table = 'user_ticket';

    public $fillable = [
        'user_id',
        'category_id',
        'bank_name',
        'bank_code',
        'tax_id',
        'email',
        'name',
        'address',
        'title',
        'ticket_type',
        'money',
        'title_category',
        'mobile',
    ];

    public $searchable = [
        'user_id',
        'ticket_id',
        'city_id',
        'code',
        'category_id',
        'remark',
    ];

    public $changeable = [
        'status'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'user_id' => 'required|int',
        'ticket_id' => 'required|int',
        'city_id' => 'required|int'
    ];

    /**
     * @param $searchOptions
     * @return mixed
     */
    public  function getQueryBySearchOptionsBackstage($searchOptions)
    {
        $qb = self::newQuery();

        unset($searchOptions['keywords']);
        if (empty($searchOptions)) {
            return $qb;
        }

        if (isset($searchOptions['money'])) {
            list($start, $end) = explode(',', $searchOptions['money']);
            $qb->where('money', '>=', $start);
            $qb->where('money', '<=', $end);
            unset($searchOptions['money']);
        }

        foreach ($searchOptions as $key => $value) {
            if (is_array($value)) {
                $qb->whereIn($key, $value);
            } elseif (is_string($value) || is_numeric($value)) {
                $qb->where($key, $value);
            }
        }

        return $qb;
    }
}
